home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / comm / ums / replydaemon.lha / ReplyDaemon.rexx
OS/2 REXX Batch file  |  1994-06-26  |  9KB  |  291 lines

  1. /* ------------------------------------------------------------------------
  2. :Program.    ReplyDaemon.rexx
  3. :Contents.   send receipt-reply messages depending on folder
  4. :Author.     hartmut Goebel [hG]
  5. :Address.    Snail Mail:              EMail:
  6. :Address.    hartmut Goebel           hartmut@oberon.nbg.sub.org
  7. :Address.    D-90459 Nürnberg
  8. :History.    $VER: ReplyDaemon.rexx 1.0 (19.6.94) {hG]
  9. :Copyright.  Public Domain
  10. :Language.   ARexx
  11. :Translator. RexxMast
  12. ------------------------------------------------------------------------ */
  13. /*
  14.  
  15. NAME
  16.      ReplyDaemon  --  send receipt-reply messages depending on folder
  17.  
  18. TEMPLATE
  19.      Name,Password,Server/K,UseReplyAccount/S,UseFolderAsReplyName/S
  20.  
  21. FUNCTION
  22.      ReplyDaemon checks for new messages within some given folders
  23.      and sends receipt-reply messages to the sender of these
  24.      messages. The checked messages will be marked as unread,
  25.      archived and postponed, thus both avoiding re-processing and
  26.      expiring.
  27.  
  28.      This is quite usefull for maintaining e.g. a bugs database where
  29.      the reporters should be given a receipt. You may use a tool like
  30.      Reciever2Folder to sort your messages into folders first and
  31.      then run ReplyDaemon to send the receipt-replies.
  32.  
  33.      The folders to be checked have to be listed in a config
  34.      variable. Also subject and body text for the message to be send
  35.      can be defined per folder via config vars.
  36.  
  37.      The receipt-reply may either be send from the users account or a
  38.      special ReplyDaemon account. The later is for avoiding garbage
  39.      messages in the user's mail folders.
  40.  
  41.      The Reply-Name put in the send message may get three values:
  42.      - the folder name (if option UseFolderAsReplyName is set)
  43.      - a reply name specified by a config var (see below)
  44.      - not set, thus later replies will get to either the user or the
  45.        ReplyDaemon, depenting on the option UsereplyAccount
  46.  
  47. OPTIONS
  48.      Name, Password, Server
  49.         are the normal UMS parameters
  50.  
  51.      UseReplyAccount
  52.         This option tells ReplyDaemon to send the messages not from
  53.         the users account but his own one. Thus it's possible to
  54.         expire the sent replies as soon as they are exported by
  55.         setting 'expire.mail = 0' the the ReplyDaeomn's account.
  56.  
  57.         The ReplyDaeomn's account must have an alias 'ReplyDaemon'
  58.         and require an empty password. Also WriteAccess must be set
  59.         correctly.
  60.  
  61.      UseFolderAsReplyName
  62.          This tells ReplyDaemon to set the ReplyName of send messages
  63.          to the name of the folder. The config-var
  64.          ReplyDaemon.ReplyName.<folder> will not be used then.
  65.  
  66. USED CONFIG-VARS
  67.  
  68.      ReplyDaemon.Folderlist          (required)
  69.           List of folders to be checked.
  70.  
  71.      ReplyDaemon.Subject.<folder>    (required)
  72.      ReplyDaemon.MsgText.<folder>    (required)
  73.           Defines the subject and body text for the send-out message.
  74.           The body text will become preceded by an address ('Hi
  75.           <FromName>') plus one empty line and appended by one emtpy
  76.           line plus the text 'Of course, this message has been
  77.           generated automaticly :-)'.
  78.  
  79.      ReplyDaemon.ReplyName.<folder>  (optional)
  80.           If the option UseFolderAsReplyName is not set, this var
  81.           determines a ReplyName to be put into the send message.
  82.  
  83. EXAMPLE
  84.  
  85.      User
  86.         Name  "Mail-Daemon"
  87.        Alias "Reply-Daemon"
  88.        Password
  89.        NetAccess       = "#?"
  90.        expire.mail     = "0"
  91.        expire.private  = "0"
  92.        .... EndUser
  93.  
  94.      User
  95.        Name  "Joe User"
  96.        Password "noone"
  97.        Alias "ju"
  98.        Alias "bugs"
  99.        Alias "info"
  100.        ....
  101.        ReplyDaemon.FolderList= "bugs*ninfo"
  102.        ReplyDaemon.ReplyName.Bugs = "ju"
  103.        ReplyDaemon.Subject.Bugs = "Recieved your bug report:"
  104.        ReplyDaemon.MsgText.Bugs= "Thank you for your bug report.*n
  105.                                   Joe User*NBugs Maintainer" ....
  106.        ReplyDaemon.Subject.Info = "Here's your Info"
  107.        ReplyDaemon.MsgText.Info= "Thank you for requesting the info*n
  108.                                   Joe User" ....
  109.      EndUser
  110.  
  111. */
  112.  
  113. /*** Startup ***/
  114.  
  115. ProgramName = "ReplyDaemon";
  116. ArgsTemplate = "Name,Password,Server/K,UseReplyAccount/S,UseFolderAsReplyName/S"
  117. ReplyName = "Reply-Daemon";
  118. ReplyPassword = "";
  119.  
  120. call addlib("hGRexxSupport.library",0,-30);
  121. interpret include("REXX:ums/UMSInit.rexx")
  122. if rc ~= 0 then do
  123.   say "cannot read UMS include-file!"
  124.   exit 20
  125. end
  126.  
  127. /*** Login ***/
  128.  
  129. replyAccount = 0;
  130.  
  131. account = UMSLogin(name, password, server)
  132. if (account = 0) then do
  133.   say "unable to login."
  134.   exit 10
  135. end
  136.  
  137. if useReplyAccount then do
  138.   replyAccount = UMSLogin(replyName, ReplyPassword, server)
  139.   if (replyAccount = 0) then do
  140.     say "unable to login."
  141.     halt 10
  142.   end
  143. end; else
  144.   replyAccount = account
  145.  
  146. FLAGS_Empty = MakeFlags()
  147. FLAG_0 = MakeFlags(0)
  148. FLAG_1 = MakeFlags(1)
  149. FLAG_2 = MakeFlags(2)
  150. FLAGS_012 = MakeFlags(0,1,2)
  151. FLAG_Old = MakeFlags(UMSUSTAT_Old)
  152. FLAGS_PostArch = MakeFlags(UMSUSTAT_PostPoned,UMSUSTAT_Archive)
  153. Match = MakeFlags(UMSUSTAT_ViewAccess)
  154. Mask  = MakeFlags(UMSUSTAT_ViewAccess,UMSUSTAT_PostPoned,UMSUSTAT_Archive, UMSUSTAT_Old)
  155.  
  156. call UMSSelectField(account, "L", FLAG_0, FLAGS_Empty,,,UMSCODE_Group, "", TRUE)
  157. call UMSSelectFlags(account, "L", FLAG_1, FLAGS_Empty,,,"U", Mask, Match)
  158.  
  159. Folderlist = GetUMSConfigVar("","Folderlist", TRUE)
  160. do forever
  161.   parse var FolderList FolderName '0A'x Folderlist
  162.   FolderName = strip(FolderName)
  163.   if FolderName = '' then leave;
  164.   call ReplyOnMsgsInFolder;
  165. end
  166.  
  167. /*** Final cleanup ***/
  168.  
  169. BREAK_C:
  170. BREAK_D:
  171. BREAK_E:
  172. BREAK_F:
  173. ERROR:
  174. HALT:
  175. IOERR:
  176. SYNTAX:
  177.  
  178. IF RC ~= 0 THEN DO
  179.   say "Error: " rc errortext(rc) "Line" sigl
  180. END
  181.  
  182. /*** Logout ***/
  183.  
  184. call UMSLogout(account)
  185. if useReplyAccount then
  186.   call UMSLogout(replyAccount)
  187. exit
  188.  
  189. /** subroutines */
  190.  
  191. ReplyOnMsgsInFolder:
  192.   MessageText = GetUMSConfigVar(FolderName,"MsgText", TRUE)
  193.   Subject     = GetUMSConfigVar(FolderName,"Subject", TRUE)
  194.   ReplyName   = GetUMSConfigVar(FolderName,"ReplyName", FALSE)
  195.  
  196.   call UMSSelectFlags(account, "L", FLAGS_Empty, FLAG_2,,,"U", FLAGS_Empty, FLAGS_Empty)
  197.   call UMSSelectField(account, "L", FLAG_2, FLAGS_Empty,,,UMSCODE_Folder, FolderName)
  198.   numTagedMsgs = UMSSelectFlags(account, "L", FLAGS_Empty, FLAGS_Empty,,,"L", FLAGS_012, FLAGS_012)
  199.  
  200.   say ProgramName || ":" numTagedMsgs "messages in folder" FolderName "found"
  201.   if numTagedMsgs = 0 then return
  202.  
  203.   i=0;last = 0
  204.   do forever
  205.     last = UMSSearchFlags(account, "L", FLAGS_012, FLAGS_012, last)
  206.     if last = 0 then leave
  207.  
  208.     drop msg. /* _IMPORTANT_ */
  209.     if ~ ReadUMSMsgHeader(account, last, msg.,true) then do
  210.       call CheckErr
  211.       exit
  212.     end
  213.  
  214.     /* test whether we should not reply */
  215.     if (find(msg.UMSCODE_Attributes,'receipt') ~= 0) then
  216.       leave
  217.  
  218.     /* generate reply */
  219.     drop newmsg.
  220.     if symbol("msg.UMSCODE_ReplyName") = "VAR" then do
  221.       newmsg.UMSCODE_ToName = msg.UMSCODE_ReplyName
  222.     end; else
  223.       newmsg.UMSCODE_ToName = msg.UMSCODE_FromName
  224.  
  225.     if abbrev(upper(newmsg.UMSCODE_ToName),"MAILINGLIST ") then
  226.       /* no reply to a mailinlist */
  227.       leave
  228.  
  229.     if symbol("msg.UMSCODE_ReplyAddr") = "VAR" then do
  230.       newmsg.UMSCODE_ToAddr = msg.UMSCODE_ReplyAddr
  231.     end; else if symbol("msg.UMSCODE_FromAddr") = "VAR" then do
  232.       newmsg.UMSCODE_ToAddr = msg.UMSCODE_FromAddr
  233.     end
  234.     if useFolderAsReplyName then do
  235.       newmsg.UMSCODE_ReplyName = FolderName;
  236.     end; else if ReplyName ~= "" then do
  237.       newmsg.UMSCODE_ReplyName = ReplyName;
  238.     end
  239.     newmsg.UMSCODE_RefID   = msg.UMSCODE_MsgID
  240.     newmsg.UMSCODE_Attributes = "receipt"
  241.     newmsg.UMSCODE_Subject = Subject msg.UMSCODE_Subject
  242.     newmsg.UMSCODE_MsgText = "Hi" msg.UMSCODE_FromName ||"!" || '0A 0A'x || MessageText ||,
  243.     '0A 0A'x || 'Of course, this message has been generated automaticly :-)'
  244.  
  245.  
  246.     if WriteUMSMsg(replyAccount, newmsg.) = 0 then
  247.       call CheckErr
  248.     else
  249.       /* mark message as postponed and archived but unread */
  250.       call UMSSelectMsg(account,"U", FLAGS_PostArch, FLAG_Old, last)
  251.       i = i+1;
  252.   end;
  253.   say i
  254.   return;
  255.  
  256. GetUMSConfigVar: procedure expose account replyAccount programname useReplyAccount
  257.   /* search order:
  258.    * User:  ProgName.varname.FolderName
  259.    * User:  ProgName.varname
  260.    * ReplyDeamon ProgName.varname
  261.    */
  262.   FolderName = arg(1);
  263.   if FolderName ~= '' then
  264.     Foldername = '.'FolderName;
  265.   varname = ProgramName || "."arg(2);
  266.   var = ReadUMSConfig(account,varname || FolderName)
  267.   if (var = "") & (FolderName ~= "") then
  268.     var = ReadUMSConfig(account,varname)
  269.   if (var = "") & useReplyAccount & (replyAccount ~= 0) then
  270.     var = ReadUMSConfig(replyAccount,varname)
  271.   if (arg(3) = TRUE) then do
  272.     if (var = '') then do
  273.       say ProgramName || ": Configuration variable '" || varname || "' is missing!"
  274.       exit
  275.     end
  276.   end
  277.   return var
  278.  
  279. /*** Support ***/
  280.  
  281. CheckErr: procedure expose account replyAccount useReplyAccount
  282.   err = UMSErrNum(account)
  283.   if err ~= 0 then do
  284.     say "UMS Error #" || err || ": " || UMSErrTxt(account)
  285.   end; else if useReplyAccount then do
  286.     err = UMSErrNum(replyAccount)
  287.     if err ~= 0 then
  288.     say "UMS Error #" || err || ": " || UMSErrTxt(replyAccount)
  289.   end
  290. return
  291.